package com.code.leetcode.before50;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/19 16:47
 */
public class The14 {

    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length < 1) return "";
        String str = strs[0];
        int index = 0;
        for (int i = 1; i < strs.length; i++) {
            index = getMaxPrefix(str, strs[i]);
            str = str.substring(0, index);
        }
        return str;
    }

    public int getMaxPrefix(String str1, String str2) {
        int minlen = Math.min(str1.length(), str2.length());
        int index = 0;
        if (minlen < 1) return index;
        for (int i = 0; i < minlen; i++) {
            if (index < minlen && str1.charAt(i) == str2.charAt(i)) {
                index ++;
            }
        }
        return index;
    }
    

}
